/**
 * @(#) WelfareListProxy.as
 *
 * @date		2009. 02. 12.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	선택적복리 후생 리스트 Proxy - Proxy
 * 				선택적복리 후생 리스트 년도별 조회
 */
package com.skt.common_hr.welfare.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.welfare.WelfareFacade;
	import com.skt.common_hr.welfare.model.vo.UsedAmountVO;
	import com.skt.common_hr.welfare.model.vo.WelfareVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
		
	public class WelfareListProxy extends CommonProxy
	{
		public static const NAME:String = "WelfareListProxy";
		
		public var welfareList:ArrayCollection = new ArrayCollection();
		public var usedAmount:UsedAmountVO;
		public var itemURL:String = "";
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function WelfareListProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * 		  year  년도
		 * @return void
		 */
		public function load(pernr:String, year:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] 		= pernr;
			param["I_GJAHR"]		= year;
			
			loadData("Z_HR_0059", param, result, fault); 
		}
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {  
				usedAmount = parseUsedMoney(event.result.OTAB1 as ArrayCollection);		//상단사용금액
				welfareList = parseWelfareList(event.result.OTAB2);		//mainList 
				itemURL = event.result["O_URL"];
				facade.sendNotification(WelfareFacade.LOAD_WELFARE_COMPLETE, this);
			}
			else {
				facade.sendNotification(WelfareFacade.LOAD_WELFARE_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			facade.sendNotification(WelfareFacade.LOAD_WELFARE_FAILED, this);
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 선택적복리후생 초기화면 list ( 상단 예산금액 )
		 */
		private function parseUsedMoney(srcArr:ArrayCollection):UsedAmountVO
		{
			var vo:UsedAmountVO = new UsedAmountVO();
			for each(var rec:Object in srcArr) 
			{
				vo.BEGDA 		= DateUtil.stringToDateTime(rec["BEGDA"]);
				vo.ENDDA 		= DateUtil.stringToDateTime(rec["ENDDA"]);
				vo.CAFBT 		= int(rec["CAFBT"] * 10 * 10);
				vo.USEBT 		= int(rec["USEBT"] * 10 * 10);
				vo.REMBT 		= int(rec["REMBT"] * 10 * 10);
			}
			return vo;	
		}
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 선택적복리후생 초기화면 mainlist
		 */
		private function parseWelfareList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			for each(var rec:Object in srcArr) 
			{
				var vo:WelfareVO = new WelfareVO();
				vo.GWAREKEY 	= rec["GWAREKEY"];
				vo.IDATE 		= DateUtil.stringToDateTime(rec["IDATE"]);
				vo.UDATE 		= DateUtil.stringToDateTime(rec["UDATE"]);
				vo.CAFGBN 		= rec["CAFGBN"];
				vo.CAFGTX 		= rec["CAFGTX"];
				vo.CAFTY 		= rec["CAFTY"];
				vo.CAFTX 		= rec["CAFTX"];
				vo.BNFAMT 		= int(rec["BNFAMT"] * 10 * 10);
				vo.BNKSA 		= rec["BNKSA"];
				vo.BNKTX 		= rec["BNKTX"];
				vo.DSTATUS 		= rec["DSTATUS"];
				vo.DSTATX 		= rec["DSTATX"];
				vo.BSTATUS 		= rec["BSTATUS"];
				vo.BSTATX 		= rec["BSTATX"];
				
				if(rec["WSTATUS"] == "" ) {
					rec["WSTATUS"] = "9";
				}
				
				vo.WSTATUS 		= rec["WSTATUS"];
				vo.WSTATX 		= rec["WSTATX"];
								
				retArr.addItem(vo);
			}
			return retArr;	
		}
		
	}
}